package com.tanhua.server.service;

import com.alibaba.fastjson.JSON;
import com.tanhua.commons.utils.Constants;
import com.tanhua.model.vo.ErrorResult;

import com.tanhua.server.exception.BusinessException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserFreezeService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 判断用户是否被冻结，被冻抛出异常
     */
    public void checkUserStatus(String state, Long userId) {
        //拼接key 从redis查询数据
        String key = Constants.FREEZE_USER + userId;
        String value = redisTemplate.opsForValue().get(key);
        //如果被冻结。抛出异常
        /**
         * USER_FREEZE_1
         * FREEZE_USER_1
         */
        if (!StringUtils.isEmpty(value)) {
            Map map = JSON.parseObject(value, Map.class);
            String freezingRange = (String) map.get("freezingRange");
            if (state.equals(freezingRange)) {
                throw new BusinessException(ErrorResult.builder().errMessage("用户已被冻结").build());
            }
        }
    }
}
